package com.sailfish.springbootdemo.dao.db5;

import com.sailfish.springbootdemo.common.DB5TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbApproveProcess;
import com.sailfish.springbootdemo.pojo.db5.SfcScdpMSnDetail;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
 * (SfcScdpMSnDetail)表数据库访问层
 *
 * @author makejava
 * @since 2023-11-10 10:57:32
 */
public interface SfcScdpMSnDetailDao {

    @Results(id = "sfcScdpMSnDetailResultMap", value = {
            @Result(column = "factory_code", property = "factoryCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "item_code", property = "itemCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "line_code", property = "lineCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "group_code", property = "groupCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "in_station_time", property = "inStationTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "factory_group_code", property = "factoryGroupCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "err_flag", property = "errFlag", jdbcType = JdbcType.VARCHAR),
            @Result(column = "first_pass", property = "firstPass", jdbcType = JdbcType.VARCHAR),
            @Result(column = "serial_number", property = "serialNumber", jdbcType = JdbcType.VARCHAR),
            @Result(column = "bt_flag", property = "btFlag", jdbcType = JdbcType.VARCHAR),
            @Result(column = "digital_line_code", property = "digitalLineCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "smtLineId", property = "smtLineId", jdbcType = JdbcType.INTEGER),
    })
    @Select("select * from " + DB5TableName.SfcScdpMSnDetail + " where id = #{id}")
    SfcScdpMSnDetail getById();

    @Select("<script> " +
            "select " +
            " factory_code, item_code, line_code, group_code, in_station_time, factory_group_code, err_flag, first_pass, serial_number, bt_flag " +
            " from " + DB5TableName.SfcScdpMSnDetail + " " +
            " where factory_group_code ='SMT' and first_pass = 0 and serial_number = #{serialNum} and in_station_time = #{in_station_time} " +
            "</script>")
    @ResultMap(value = "sfcScdpMSnDetailResultMap")
    SfcScdpMSnDetail getOneByLimit(@Param("serialNum") String serialNum, @Param("in_station_time") Date in_station_time);

    @Select("<script> " +
            "select " +
            " factory_code, item_code, line_code, group_code, in_station_time, factory_group_code, err_flag, first_pass, serial_number, bt_flag " +
            " from " + DB5TableName.SfcScdpMSnDetail + " " +
            " where factory_group_code = 'SMT' and first_pass = '0' and item_code = #{code} and in_station_time >= #{startTime} " +
            " order by in_station_time " +
            " limit #{offset}, #{limit} " +
            "</script>")
    @ResultMap(value = "sfcScdpMSnDetailResultMap")
    List<SfcScdpMSnDetail> getByLimit(@Param("code") String code, @Param("startTime") Date startTime,
                                      @Param("offset") int offset, @Param("limit") int limit);

    @Select("<script> " +
            "select " +
            "a.*,b.factory_name, b.digital_line_code,b.sfc_line_code,b.smtLineId from " +
            " (select factory_code,item_code,line_code,max(in_station_time) as in_station_time from " + DB5TableName.SfcScdpMSnDetail +
            " where item_code = #{code} and factory_code = #{factoryCode} and factory_group_code='SMT' " +
            " group by factory_code,item_code,line_code) a "+
            " join h3c_capacity_center_pcba.tb_factory_info b " +
            " on a.factory_code=b.factory_code and a.line_code = b.sfc_line_code" +
            "</script>")
    @ResultMap(value = "sfcScdpMSnDetailResultMap")
    List<SfcScdpMSnDetail> getByLimit2(@Param("code") String code, @Param("factoryCode") String factoryCode);

    @Select("<script> " +
            "select " +
            "distinct item_code " +
            "from " + DB5TableName.SfcScdpMSnDetail +
            "</script>")
    List<String> getCodeList();

    @Insert("insert into " + DB5TableName.SfcScdpMSnDetail + "(factory_code, item_code, line_code, group_code, in_station_time, factory_group_code, err_flag, first_pass, serial_number, bt_flag) " +
            "values (#{factoryCode}, #{itemCode}, #{lineCode}, #{groupCode}, #{inStationTime}, #{factoryGroupCode}, #{errFlag}, #{firstPass}, #{serialNumber}, #{btFlag})")
    Integer insert(SfcScdpMSnDetail sfcScdpMSnDetail);
}

